#!/system/bin/sh
[[ -e $TMPDIR/*Beta* ]] && set -x

TuiChu() { echo $@; exit 1; }

local Jin DXH_SD FaXian YuMing DXH_ID DXH_Shu DXH_Liang DXH_Id DXH_DIR DXH_Dir DXH_Dir_2 DXH_Dir_3 DXH_SuiJi DXH_Magisk DXH_FenGeXian DXH_ShiJian_KaiShi model model_version ua YuMing GeShi
[ -d /sdcard/?ndroid ] && DXH_SD=/sdcard || DXH_SD=/storage/emulated/0
[ -f $DXH_SD/?ownload/?ownload ] && source $DXH_SD/?ownload/?ownload && FaXian="发现文件，正在读取…… [`cat $DXH_SD/?ownload/?ownload`]"
[[ ! -d $2 ]] && DXH_DIR=$DXH_SD/.DXH || DXH_DIR=$2
[[ ! -d $DXH_DIR ]] && mkdir -p $DXH_DIR
[[ -z $DXH_ID ]] && DXH_ID=$$
[[ -z $DXH_Shu ]] && DXH_Shu=$#
[[ -z $DXH_Liang ]] && DXH_Liang=$@
[[ -z $DXH_Id ]] && DXH_Id=${0##*/}
[[ -z $DXH_Dir ]] && DXH_Dir=${0%/*}
[[ -z $DXH_Dir_2 ]] && DXH_Dir_2=`pwd`
[[ -z $DXH_Dir_3 ]] && DXH_Dir_3=`dirname "$0"`
[[ -z $DXH_SuiJi ]] && DXH_SuiJi=$((RANDOM %3))
[[ -z $DXH_Magisk ]] && DXH_Magisk=/data/data/com.topjohnwu.magisk/cache/flash
[[ -z $DXH_FenGeXian ]] && DXH_FenGeXian=---------------------------------------------------------
[[ -z $DXH_ShiJian_KaiShi ]] && DXH_ShiJian_KaiShi=`date "+%s"`

[[ -z $model ]] && model=`getprop ro.product.model`
[[ -z $model_version ]] && model_version=`getprop ro.build.version.release`
model=${model:-MI 8 Lite}
model_version=${model_version:-10}
ua="User-Agent: Mozilla/5.0 (Linux; Android ${model_version}; ${model}) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.162 Mobile Safari/537.36"

[[ `id -u` != 0 ]] && TuiChu "- 请使用root状态运行！！！"
[[ $DXH_Id != ?ownload* && $DXH_Id != dxh* ]] && TuiChu "- 请勿修改名称！！！"
[[ $DXH_SuiJi = 0 ]] && [[ -z $GeShi ]] && GeShi=py
[[ $DXH_SuiJi = 1 ]] && [[ -z $GeShi ]] && GeShi=ini
[[ $DXH_SuiJi = 2 ]] && [[ -z $GeShi ]] && GeShi=h
[[ -z $DXH_Dir ]] && DXH_Dir=获取失败

DXH() {
echo "- 路径：$DXH_Dir
- 作者：酷安@董小豪
- 版本：2020052011 10:27:04"
}

download() {
export PATH=/sbin/.magisk/busybox:/data/data/com.termux/files/usr/bin:/data/data/bin.mt.plus/files/term/usr/bin:$PATH:$RUN
local Download_Dir Download_Name Download_Name_2 Download_ID Download_ID_2 Download_DaXiao Download_DaXiao_2 Download_MD5 LianJie WanCheng Download_DIR TMPDIR_2 curl WenJian MD5 CunZai Han Shu DaXiao DaXiao_2 Shu MBK MBK_2 JinCheng MBK_SuDu ShengYu YanSe YanSe_2 SuDu WangZhan WangZhan_2 Download_ID_Name_DaXiao Download_ID_Name_DaXiao_2 DaXiao_3 DaXiao_4

[[ -z $Download_Dir ]] && Download_Dir=$1
[[ -z $Download_Name ]] && Download_Name=$2
[[ -z $Download_ID ]] && Download_ID=$3
[[ -z $Download_DaXiao ]] && Download_DaXiao=$4
[[ -z $Download_MD5 ]] && Download_MD5=$5
[[ -z $LianJie ]] && LianJie=$Download_Dir/LianJie
[[ -z $WanCheng ]] && WanCheng=$Download_Dir/WanCheng
[[ -z $Download_DIR ]] && Download_DIR=$Download_Dir/$Download_Name
[[ ! -d $Download_Dir ]] && DXH_DIR=$DXH_SD/.DXH
[[ ! -d $DXH_DIR ]] && mkdir -p $DXH_DIR

Download_ID_2=`echo -n $Download_ID | wc -c`
Download_DaXiao_2=`echo -n $Download_DaXiao | wc -c`
[[ $Download_ID_2 -ge 9 ]] && [[ $Download_ID != *http* ]] && Download_ID=`echo -n $Download_ID | base64 -d`
[[ $Download_ID = *http* ]] && YuMing=${Download_ID#*//} && YuMing=${YuMing%%lanzous*}
[[ $Download_ID_2 -ge 8 ]] && Download_ID=${Download_ID:0-7}
[[ $Download_MD5 -ge 33 ]] && [[ `echo -n $Download_MD5 | grep ^[A-Za-z0-9=]` ]] && Download_MD5=`echo -n $Download_MD5 | base64 -d`
[[ $Download_DaXiao_2 -ge 33 ]] && [[ `echo -n $Download_DaXiao | grep ^[A-Za-z0-9=]` ]] && Download_MD5=`echo -n $Download_MD5 | base64 -d`
[[ $Download_DaXiao_2 -ge 10 ]] && [[ `echo -n $Download_DaXiao | grep ^[A-Za-z0-9]` ]] && Download_MD5=$Download_DaXiao && Download_DaXiao=$Download_MD5

echo_color() {
local JinCheng

JinCheng=${JinCheng%.*}
if [[ ! -d $DXH_Magisk ]]; then
	if [[ $JinCheng -le 20 ]]; then
		let DeiJing=5
		let ZiTi=34
	elif [[ $JinCheng -le 45 ]]; then
		let DeiJing=5
		let ZiTi=33
	elif [[ $JinCheng -le 75 ]]; then
		let DeiJing=5
		let ZiTi=31
	else
		let DeiJing=5
		let ZiTi=32
	fi
YanSe="\e[${DeiJing:-5};${ZiTi}m"
YanSe_2="\e[0m"
fi
}

WangZhan() {
local Download_ID_2

sleep 1
echo -n "- 连接中 "
YuMing=${YuMing:-www.}
MIUI_version=`getprop ro.miui.ui.version.name`
MIUI_incremental=`getprop ro.build.version.incremental`
WangZhan="https://${YuMing}lanzous.com/$Download_ID"
WangZhan_2="https://${YuMing}lanzous.com/tp/$Download_ID"
echo -n "❶ " && Download_ID_Name_DaXiao=`$curl -s -m 3 -A "$ua" $WangZhan | grep '"m[d-h]' | sed -e 's#.*mf.*##g'`
[[ -z $Download_ID_Name_DaXiao ]] && [[ -z $Shu ]] && Download_ID_Name_DaXiao_2=`$curl -s -m 3 -A "$ua" $WangZhan | grep '"off' | sed -e 's#</div>##g; s#.*1">##g'`
[[ -n $Download_ID_Name_DaXiao ]] && [[ -z $Shu ]] && echo -n "❷ " && Download_ID_2=`echo -n $Download_ID_Name_DaXiao | grep "$Download_ID" | sed -e 's#.*/tp/##g; s#" t.*##g'`
[[ -n $Download_ID_Name_DaXiao ]] && [[ -z $Shu ]] && echo -n "❸ " && Download_Name_2=`echo -n $Download_ID_Name_DaXiao | sed -e 's#.*md">##g; s# <.*##g'`
[[ -n $Download_ID_Name_DaXiao ]] && [[ -z $Shu ]] && echo -n "❹ " && Download_DaXiao_2=`echo -n $Download_ID_Name_DaXiao | sed -e 's#.*mtt"##g; s#)<.*##g; s#>(##g; s# ##g'`
[[ ${Download_ID_2:-0} != $Download_ID ]] && echo 1 > $LianJie && echo && echo "- 连接异常！！！" && return 1 || echo 0 > $LianJie
[[ -z $Shu ]] && Download_DIR=$Download_Dir/$Download_Name_2 && Download_Name=$Download_Name_2
[[ -n $Download_ID_Name_DaXiao ]] && echo && echo "- 连接成功"
}

MD5() {
local MD5

if [[ -n $Download_MD5 ]]; then
	MD5=`md5sum $Download_DIR | sed 's/ .*//g'`
	if [[ $MD5 != $Download_MD5 ]]; then
#		rm -f $Download_DIR
		echo "MD5校验[$Download_Name] 失败！！！"
		rm -f $LianJie $WanCheng
		return 1
	else
		return 0
	fi
fi
}

XiaZai() {
local DXH UA UA_2

sleep 1
echo -n "- 连接中 "
echo -n "❶ " && DXH=https://vip.d0.baidupan.com/file/`$curl -s -k -A "$ua" https://lanzous.com/tp/icoq7ti | egrep "\"\?|'\?" | sed "s/'//g;"'s/"//g;s/.*?/?/g'`
[[ -n $DXH ]] && echo -n "❷ " && $curl -s -k -L -H "Accept-language: zh-CN,zh;q=0.9,en;q=0.8,zh-HK;q=0.7" $DXH -o $DXH_SD/?ownload/DXH.txt
[ -f $DXH_SD/?ownload/DXH.txt ] && rm -f $DXH_SD/?ownload/DXH.txt
echo -n "❸ " && UA=https://vip.d0.baidupan.com/file/`$curl -s -k -A "$ua" $WangZhan_2 | egrep "\"\?|'\?" | sed "s/'//g;"'s/"//g;s/.*?/?/g'`
UA_2=`echo -n $UA | wc -c`
[[ $UA_2 -ge 34 ]] && echo 0 > $LianJie || echo 1 > $LianJie
[[ `cat "$LianJie"` = 0 ]] && echo "❹ " && echo "- 连接成功" && $curl -s -k -L -H "Accept-language: zh-CN,zh;q=0.9,en;q=0.8,zh-HK;q=0.7" $UA -o $Download_DIR
[[ `cat "$LianJie"` = 0 ]] && [[ -f $Download_DIR ]] && echo 0 > $WanCheng
[[ `cat "$LianJie"` = 1 ]] && echo "- 连接异常！！！\n- $Download_ID_Name_DaXiao_2" && rm -f $LianJie && return 1
[[ -z $Download_DaXiao ]] && rm -f $LianJie $WanCheng
}

TMPDIR_2=$TMPDIR
if [ -f $TMPDIR_2/*/curl ]; then
	curl=$TMPDIR_2/*/curl
elif [ -f $TMPDIR_2/curl ]; then
	curl=$TMPDIR_2/curl
elif [ -f `which curl` ]; then
	curl=`which curl`
else
	echo "- curl异常！！！"
	return 1
fi

touch $Download_Dir/CeShi
if [[ $? != 0 ]]; then
	echo
	rm -f $Download_Dir/CeShi
	echo "- 下载保存路径异常！！！"
	return 1
else
	rm -f $Download_Dir/CeShi
fi

WiFi=$(ifconfig wlan0 | grep 'inet addr:' | awk '{printf $2}' | awk -F: '{print $2}')
[[ -z $WiFi ]] && ShuJu=`ping -c 1 -w 1 lanzous.com`
[[ -n $WiFi ]] && [[ -z $ZhuangTai ]] && ZhuangTai="无线网络状态"
[[ -n $ShuJu ]] && [[ -z $ZhuangTai ]] && ZhuangTai="非无线网络状态"

echo $DXH_FenGeXian
[[ -n $ZhuangTai ]] && echo "- $ZhuangTai"
WangZhan

if [[ -n $Download_DaXiao && -f $Download_DIR ]]; then
	WenJian=`wc -c < $Download_DIR`
	if [[ $WenJian != $Download_DaXiao ]]; then
		CunZai=$?
		rm -f $Download_DIR
	else
		CunZai=$?
		rm -f $LianJie $WanCheng
	fi
fi

if [[ -n $Download_MD5 && -f $Download_DIR ]]; then
	MD5=`md5sum $Download_DIR | sed 's/ .*//g'`
	if [[ $MD5 != $Download_MD5 ]]; then
		CunZai=$?
		rm -f $Download_DIR
	else
		CunZai=$?
		rm -f $LianJie $WanCheng
	fi
fi

[[ -z $Han ]] && Han=0
[[ -z $Shu ]] && Shu=0
[[ -z $Download_ID_Name_DaXiao ]] && [[ $Download_Name -ge 0 ]] && Download_Name=未知

if [[ -z $Download_DaXiao ]]; then
	[[ $Download_DaXiao_2 != 0 ]] && DaXiao=估计$Download_DaXiao_2 || DaXiao=未知
elif [[ $Download_DaXiao -ge 1073741824 ]]; then
	DaXiao=`awk "BEGIN{print $Download_DaXiao/1073741824}"`GB
elif [[ $Download_DaXiao -ge 1048576 ]]; then
	DaXiao=`awk "BEGIN{print $Download_DaXiao/1048576}"`MB
elif [[ $Download_DaXiao -ge 1024 ]]; then
	DaXiao=`awk "BEGIN{print $Download_DaXiao/1024}"`KB
elif [[ $Download_DaXiao -le 1024 ]]; then
	DaXiao=${Download_DaXiao}B
fi

#[[ -n $Download_DaXiao_2 ]] && DaXiao_3=估计`du -sh $Download_DIR | awk '{print $1}'`
[[ -n $DaXiao_3 ]] && [ $DaXiao_3 = $DaXiao ] && CunZai=1

[[ $CunZai = 1 ]] && echo "- [$Download_Name] 文件已存在！！！" && return 1

XiaZai &

echo "$DXH_FenGeXian\n- 正在下载：$Download_Name\n- 文件大小：$DaXiao"

if [[ -n $Download_DaXiao ]]; then
	until [[ $Han = 1 ]]; do
		let Shu++
		Shu=$Shu
		[[ -f $Download_DIR ]] && MBK=`wc -c < $Download_DIR`
		sleep 1
		if [[ -f $Download_DIR ]]; then
			MBK_2=`wc -c < $Download_DIR`
			if [[ $MBK_2 -ge 1073741824 ]]; then
				DaXiao_2=`awk "BEGIN{print $MBK_2/1073741824}"`GB
			elif [[ $MBK_2 -ge 1048576 ]]; then
				DaXiao_2=`awk "BEGIN{print $MBK_2/1048576}"`MB
			elif [[ $MBK_2 -ge 1024 ]]; then
				DaXiao_2=`awk "BEGIN{print $MBK_2/1024}"`KB
			elif [[ $MBK_2 -le 1024 ]]; then
				DaXiao_2=${MBK_2}B
			fi
		fi

		[[ -n $Download_DaXiao ]] && [[ -n $MBK_2 ]] && JinCheng=`awk "BEGIN{print $MBK_2/($Download_DaXiao/100)}"`
		[[ -n $MBK ]] && [[ -n $MBK_2 ]] && MBK_SuDu=$(($MBK_2-$MBK))
		[[ -n $Download_DaXiao ]] && [[ -n $MBK_2 ]] && [[ -n $MBK_SuDu ]] && ShengYu=`awk "BEGIN{print ($Download_DaXiao-$MBK_2)/$MBK_SuDu}" 2>/dev/null`
		ShengYu=${ShengYu:-未知}

		if [[ ! -d $DXH_Magisk ]]; then
			if [[ $DXH_SuiJi = 2 ]]; then
				((ZiTi=RANDOM%7+31))
				YanSe="\e[${DeiJing:=5};${ZiTi}m"
				YanSe_2="\e[0m"
			elif [[ $DXH_SuiJi = 1 ]]; then
				ShuZi=${JinCheng%.*}
				((ZiTi=30+ShuZi%8))
				YanSe="\e[${DeiJing:=5};${ZiTi}m"
				YanSe_2="\e[0m"
			elif [[ $DXH_SuiJi = 0 ]]; then
				echo_color
			fi
		fi

		if [[ -z $MBK_SuDu ]]; then
			SuDu=下载速度：未知
		elif [[ $MBK_SuDu -ge 1073741824 ]]; then
			SuDu=下载速度：`awk "BEGIN{print $MBK_SuDu/1073741824}"`GB/s
		elif [[ $MBK_SuDu -ge 1048576 ]]; then
			SuDu=下载速度：`awk "BEGIN{print $MBK_SuDu/1048576}"`MB/s
		elif [[ $MBK_SuDu -ge 1024 ]]; then
			SuDu=下载速度：`awk "BEGIN{print $MBK_SuDu/1024}"`KB/s
		elif [[ $MBK_SuDu -lt 1024 ]]; then
			SuDu=下载速度：${MBK_SuDu}B/s
		fi

		[[ -n $Jin ]] && [[ -n $MBK_2 ]] && echo "$DXH_FenGeXian \n$YanSe- 已下载：$DaXiao_2/$DaXiao 已完成${JinCheng}% \n- $SuDu 剩余时间：$ShengYu/s $ShiJian $YanSe_2"
		[[ $Shu -ge 10 ]] && [[ ! -e $Download_DIR ]] && Han=1
		[[ $Shu -ge 10 ]] && [[ `cat "$LianJie"` = 1 ]] && MD5 && Han=1
		[[ -f $WanCheng ]] && [[ `cat "$WanCheng"` = 0 ]] && MD5 && Han=1
	done
fi
rm -f $LianJie $WanCheng
}

case $1 in

	-x)
		download $DXH_DIR $3 $4 $5 $6
	;;

	-xj|-jx)
		[[ -z $Jin ]] && Jin=$1
		download $DXH_DIR $3 $4 $5 $6
	;;

	-xs|-sx)
		[[ -z $Jin ]] && Jin=$1
        echo "下载日志保存在$DXH_SD/[`date '+%F %T'`]-调试日志.$GeShi"
		exec 2>> $DXH_SD/[`date '+%F %T'`]-调试日志.$GeShi
		set -x
		set +euo pipefail
		download $DXH_DIR $3 $4 $5 $6
	;;

	*)
		[[ -n $1 ]] && echo "- 指令错误：[ $1 ]！！！"
		if [[ ! -d $DXH_Magisk ]]; then
			DXH
		fi
	;;

esac

set +euxo pipefail
